package dao

import (
	"course/global"
	"gorm.io/gorm"
	"time"
)

type Course struct {
	gorm.Model
	Name    string    `gorm:"type:varchar(20);index;comment:课程名称"`
	Status  int       `json:"status" gorm:"size:1;index;default:0;comment:状态 0-正常 1-下架"`
	Coach   string    `json:"coach"` //教练
	Price   float64   `json:"price"` //价格
	Start   time.Time `json:"start"`
	End     time.Time `json:"end"`     //开始 结束时间
	Space   string    `json:"space"`   //场馆
	Address string    `json:"address"` //场地
}

type Reservation struct {
	ID        uint      `gorm:"primarykey"`
	UserID    int       `json:"user_id"`
	Course    string    `json:"course"` //课程名称
	Data      time.Time `json:"data"`
	Time      time.Time `json:"time"`
	Space     string    `json:"space"`      //场馆
	Address   string    `json:"address"`    //场地
	PersonNum int       `json:"person_num"` //成团人数
	Num       int       `json:"num"`        //预约人数
	Desc      string    `json:"desc"`

	Status int `json:"status" gorm:"size:1;index;default:0;comment:预约状态 0-正常 1-取消"` //预约状态 0-正常 1-取消

}

func (c *Course) TableName() string {
	return "course"
}
func (c *Reservation) TableName() string {
	return "reservation"
}

func (c *Course) GetAllCourseInfo() []Course {
	var course []Course
	global.DB.Where("status = ?", 0).Find(&course)
	return course
}
func (c *Course) GetDetailCourseInfo(id int) (Course, error) {
	var course Course
	err := global.DB.Where("id = ? AND status=?", id, 0).First(&course).Error
	if err != nil {
		return Course{}, err
	}
	return course, nil
}
func (c *Reservation) GetReservation(id int) (*Reservation, error) {
	var res Reservation
	err := global.DB.Where("id  = ?", id).First(&res).Error
	if err != nil {
		return nil, err
	}
	return &res, nil
}
func (c *Reservation) GetUserAllReservationInfo(userID, status int) []Reservation {
	var res []Reservation
	global.DB.Where("user_id = ? AND status = ?", userID, status).Find(&res)
	return res
}
